<?php
class Guest extends AppModel {
    public $name         = 'Guest';
    public $displayField = 'first_name';
    public $validate     = array(
        'title' => array(
            'rule'     => 'notEmpty',
            'required' => true,
        ),
        'firstname' => array(
            'rule'     => 'notEmpty',
            'required' => true,
        ),
        'lastname' => array(
            'rule'     => 'notEmpty',
            'required' => true
        ),
        'country' => array(
            'rule'     => 'notEmpty',
            'required' => true
        ),
        'email' => array(
            'isEmail' => array(
                'rule'       => 'email',
                'required'   => true,
                'allowEmpty' => true,
                'message'    => 'Please supply a valid email address'
            )
        )
    );

    public function guestList ($params) {
        $page = $params['page'];
        $cond = 'g.`status`="A" ';

        if ($params['query'] && $params['qtype']) {
            $cond .= 'AND g.`'.$params['qtype'].'` LIKE "%'.$params['query'].'%" ';
        }

        $sql = 'SELECT COUNT(*) AS total FROM `guests` AS g ' .
               'WHERE '.$cond;
        $ttl = $this->query($sql);

        $total = (count($ttl) == 0) ? 0 : $ttl[0][0]['total'];
        if ($total == 0) {
            return array(
                'page'  => 1,
                'total' => 0,
                'rows'  => array()
            );
        }

        $rpp   = isset($params['rp']) ? $params['rp'] : 10;
        $start = (($page-1) * $rpp);
        $sort  = empty($params['sortname']) ? 'id' : $params['sortname'];
        $ord   = (empty($params['sortorder']) || $params['sortorder'] == 'asc') ? 'ASC' : 'DESC';
        $sql   = 'SELECT g.* FROM `guests` AS g ' .
                 'WHERE '.$cond .
                 'ORDER BY g.`'.$sort.'` '.$ord.' ' .
                 'LIMIT '.$start.', '.$rpp;
        $data = $this->query($sql);
        $rec  = array();

        if (count($data) == 0) {
            return array(
                'page'  => 1,
                'total' => 0,
                'rows'  => array()
            );
        }

        foreach ($data as $rs) {
            $id = $rs['g']['id'];

            $lastStay = ($rs['g']['last_stay']) ? $this->serverToClientTime($rs['g']['dob'], 'd/m/Y') : '-';
            $created  = $this->serverToClientTime($rs['g']['created'], 'd/m/Y H:i a');
            $modified = $this->serverToClientTime($rs['g']['modified'], 'd/m/Y H:i a');
            $actions  = '<div class="selectRecord" onclick="selectGuestRecord('.$id.');"></div>' .
                        '<div class="editRecord" onclick="editGuestProfile('.$id.');"></div>';

            $cell = array(
                $rs['g']['id'], $rs['g']['title'], $rs['g']['firstname'], $rs['g']['lastname'], $rs['g']['street'], $rs['g']['city'], $rs['g']['state'],
                $rs['g']['zip_code'], $rs['g']['country'], $rs['g']['phone'], $rs['g']['email'], $rs['g']['password'], $lastStay, $created, $modified, $actions
            );

            $rec[] = array(
                'id'   => $rs['g']['id'],
                'cell' => $cell
            );
        }

        return array(
            'page'  => $page,
            'total' => $total,
            'rows'  => $rec
        );
    } // guestList
} // Guest
?>